#pragma once
#include "LoadingHandle.h"

namespace Loading{
	template<typename T>
	class Loader : public boost::enable_shared_from_this<Loader<T>>{
	public:
		Loader(){}
		virtual ~Loader() {}

		virtual boost::shared_ptr<T> load() = 0;
		boost::shared_ptr<LoadingHandle<T>> loadAsync() { return loadResourceAsync(shared_from_this()); }

	private:
		template <typename T>
		static boost::shared_ptr<LoadingHandle<T>> loadResourceAsync(boost::shared_ptr<Loader<T>> loader){
			boost::shared_ptr<LoadingHandle<T>> handle = boost::shared_ptr<LoadingHandle<T>>(new LoadingHandle<T>);

			boost::shared_ptr<ThreadLib::Thread> thread = ThreadLib::ThreadFactory::spawnThread(boost::bind(&loadAsyncFunc<T>,handle,loader));
			thread->run();

			return handle;
		}

		template <typename T>
		static void loadAsyncFunc(boost::shared_ptr<LoadingHandle<T>> handle, boost::shared_ptr<Loader<T>> loader){
			handle->m_handle = loader->load();
		}
	};
}